/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    sendAjax('/v1/teacher/apart/detail.json', 'get', {}, function(json){
        var user_apart = json.body.data;
        if (user_apart) {
            loadList('/v1/teacher/repair/list.json', user_apart);
        }
    });
    $('#edit-repair-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var apart_id = button.data('apart-id');
        var apart_name = button.data('apart-name');
        var type = button.data('type');
        var content = button.data('content');
        if (id) {
            $(this).find('.repair-id').val(id);
        }
        if (type) {
            $(this).find('.repair-type').val(type);
            $(this).find('.repair-type option').each(function(){
                if ($(this).val() == type) {
                    console.log(type, $(this).val());
                    $(this).attr('selected', 'selected');
                }
            });
        }
        $(this).find('.apart-id').val(apart_id);
        $(this).find('.apart-name').text(apart_name);
        if (content) {
            $(this).find('.repair-content').val(content);
        }
    });
    $('#edit-repair-dialog').on('hide.bs.modal', function(){
        $(this).find('.repair-id').val('');
        $(this).find('.apart-id').val('');
        $(this).find('.apart-name').text('');
    });
    $('#edit-repair-dialog .btn-save').on('click', function(){
        var that = $('#edit-repair-dialog');
        var data = {
            apart_id: that.find('.apart-id').val(),
            type: that.find('.repair-type').val(),
            content: that.find('.repair-content').val(),
            status: that.find('.repair-status').val()
        };
        if (that.find('.repair-id').val() != '') {
            data.id = that.find('.repair-id').val();
        }
        sendAjax('/v1/teacher/save/repair', 'post', {data:data}, function(json){
            that.modal('hide');
            $('#action-tip .tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });
    $('#view-repair-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var that = $(this);
        sendAjax('/v1/teacher/repair/detail.json', 'get', {id:id}, function(json){
            data = json.body.data;
            var repair_type = getRepairType(data.type);
            that.find('.apart-name').text(data.name);
            that.find('.reply-at').text(data.reply_at);
            that.find('.repair-type').text(repair_type);
            that.find('.content').text(data.content);
            var status = '';
            switch (data.status) {
                case 1 : status = '<span class="text-default"><b>未回复</b></span>';
                case 2 : status = '<span class="text-warning"><b>处理中</b></span>';
                case 3 : status = '<span class="text-success"><b>已完成</b></span>';
                default : status = '<span class="text-default"><b>未回复</b></span>';
            }
            that.find('.status').html(status);
            that.find('.remark').text(data.remark);
        });
    });
    $('#view-repair-dialog').on('hide.bs.modal', function(){
        $(this).find('.apart-name').text('');
        $(this).find('.reply-at').text('');
        $(this).find('.repair-type').text('');
        $(this).find('.content').text('');
        $(this).find('.status').html('');
        $(this).find('.remark').text('');
    });

    $('#delete-repair-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        $('.repair-id').val(id);
        $('.apart-name').text(name);
    });
    $('#delete-repair-dialog').on('hide.bs.modal', function(){
        $('.repair-id').val('');
        $('.apart-name').text('');
    });
    $('#delete-repair-dialog .btn-save').on('click', function(){
        var id = $('.repair-id').val();
        sendAjax('/v1/teacher/delete/repair', 'post', {id:id}, function(json){
            $('#delete-repair-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url, user_apart) {
    $("#repair-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: '<a class="btn btn-success edit-repair" data-toggle="modal" data-target="#edit-repair-dialog" data-apart-id="'+user_apart.apart_id+'" data-apart-name="'+user_apart.apart_name+'">添加</a>',
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'apart_id',
            title: '公寓ID',
            visible: false
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'type',
            title: '报修类型',
            formatter: function (value) {
                return getRepairType(value);
            }
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '<span class="text-default"><b>未回复</b></span>';
                    case 2 : return '<span class="text-warning"><b>处理中</b></span>';
                    case 3 : return '<span class="text-success"><b>已完成</b></span>';
                    default : return '<span class="text-default"><b>未回复</b></span>';
                }
            }
        }, {
            field: 'reply_at',
            title: '回复时间'
        }, {
            field: 'content',
            title: '报修内容',
            visible: false
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-repair" data-toggle="modal" data-target="#view-repair-dialog" data-id="'+row.id+'">详情</a>';
                if (row.status == 1) {
                    action += '<a class="btn btn-xs btn-success edit-repair" data-toggle="modal" data-target="#edit-repair-dialog" data-id="'+row.id+'" data-apart-id="'+row.apart_id+'" data-apart-name="'+row.name+'" data-type="'+row.type+'" data-content="'+row.content+'">编辑</a>';
                    action += '<a class="btn btn-xs btn-danger delete-repair" data-toggle="modal" data-target="#delete-repair-dialog" data-id="'+row.id+'">删除</a>';
                }
                return action;
            }
        }]
    });
}

function getRepairType(type) {
    switch (type) {
        case 0 : return '其他服务';
        case 1 : return '水电维修';
        case 2 : return '家电维修';
        case 3 : return '管道疏通';
        case 4 : return '空调维修';
        case 5 : return '开锁换锁';
        case 6 : return '内饰维修';
        case 7 : return '家具维修';
        case 8 : return '防水补漏';
        default : return '其他服务';
    }
}